package dk.marten.ibdrecord;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
//import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.format.DateFormat;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "IBDRecord.db";
	private static final int DATABASE_VERSION = 1;

	public static final String TABLE_TILSTAND = "Tilstand";
	public static final String TABLE_TOILET = "Toilet";
	
	public static final String ID = "id";
	public static final String DATO = "regDato";

	public static final String WEIGHT = "weight";
	public static final String TILSTAND = "tilstand";
	public static final String FATIGUE = "fatigue";
	public static final String SMERTE = "smerte";
	public static final String STRESS = "stress";
	public static final String[] TABLE_TILSTAND_COLUMNS = {ID,DATO,WEIGHT,TILSTAND,FATIGUE,SMERTE,STRESS};
	
	public static final String KONSISTENS = "konsistens";
	public static final String SLIM = "slim";
	public static final String BLOD = "blod";
	public static final String TRANG = "trang";
	public static final String[] TABLE_TOILET_COLUMNS = {ID,DATO,KONSISTENS,SLIM,BLOD,TRANG};

	public static final String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
	
	private static DBHelper mInstance = null;
	  
	public static DBHelper getInstance(Context ctx) {
		if (mInstance == null) {
			mInstance = new DBHelper(ctx.getApplicationContext()); 
			}
		return mInstance;
	}

	private DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table "+TABLE_TOILET+" ("+ID+" integer primary key autoincrement,"
				+DATO+" DATETIME, "
				+KONSISTENS+" INTEGER, "
				+SLIM+" INTEGER, "
				+BLOD+" INTEGER, "
				+TRANG+" INTEGER);");
		
		db.execSQL("create table "+TABLE_TILSTAND+" ("+ID+" integer primary key autoincrement,"
				+DATO+" DATETIME, "
				+WEIGHT+" REAL, "
				+TILSTAND+" INTEGER, "
				+FATIGUE+" INTEGER, "
				+SMERTE+" INTEGER, "
				+STRESS+" INTEGER);");
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
	}
	
	  public static String calenderToString(Calendar regDato) {
	    return (String) DateFormat.format(DATE_FORMAT, regDato);
	  }
	  
	/**
	 * 
	 * @param strDato
	 * @return
	 */
	  public static Calendar stringToCalendar(String strDato) {		
		strDato = strDato.replace(".xxxxxx", "");
		Calendar calendar = Calendar.getInstance();
		try {
			SimpleDateFormat f = new SimpleDateFormat(DBHelper.DATE_FORMAT);
			calendar.setTime(f.parse(strDato));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	    return calendar;
	  }

}
